package com.dianping.cat.home.jar.transform;
import com.dianping.cat.home.jar.IVisitor;
import com.dianping.cat.home.jar.entity.Domain;
import com.dianping.cat.home.jar.entity.Jar;
import com.dianping.cat.home.jar.entity.JarReport;
import com.dianping.cat.home.jar.entity.Machine;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;

  public DefaultNativeParser(InputStream in) {
/*  16 */     this.m_linker = new DefaultLinker(true);




/*  21 */     this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;

  public static JarReport parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }


  public static JarReport parse(InputStream in) {
    DefaultNativeParser parser = new DefaultNativeParser(in);
/*  30 */     JarReport jarReport = new JarReport();

    try {
/*  33 */       jarReport.accept(parser);
/*  34 */     } catch (RuntimeException e) {
/*  35 */       if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {

/*  38 */         throw e;
      }
    }

/*  42 */     parser.m_linker.finish();
/*  43 */     return jarReport;
  }



  public void visitDomain(Domain domain) {
    byte tag;
/*  50 */     while ((tag = readTag()) != -1) {
/*  51 */       visitDomainChildren(domain, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitDomainChildren(Domain domain, int _field, int _type) {
/*  56 */     switch (_field) {
      case 1:
/*  58 */         domain.setId(readString());
        break;
      case 33:
/*  61 */         if (_type == 1) {
/*  62 */           Machine machine = new Machine();

/*  64 */           visitMachine(machine);
/*  65 */           this.m_linker.onMachine(domain, machine); break;
/*  66 */         }  if (_type == 2) {
/*  67 */           for (int i = readInt(); i > 0; i--) {
/*  68 */             Machine machine = new Machine();

/*  70 */             visitMachine(machine);
/*  71 */             this.m_linker.onMachine(domain, machine);
          }
        }
        break;
    }
  }



  public void visitJar(Jar jar) {
    byte tag;
/*  82 */     while ((tag = readTag()) != -1) {
/*  83 */       visitJarChildren(jar, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitJarChildren(Jar jar, int _field, int _type) {
/*  88 */     switch (_field) {
      case 1:
/*  90 */         jar.setId(readString());
        break;
      case 2:
/*  93 */         jar.setVersion(readString());
        break;
    }
  }



  public void visitJarReport(JarReport jarReport) {
    byte tag;
/* 102 */     if ((tag = readTag()) != -4) {
/* 103 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }

/* 106 */     while ((tag = readTag()) != -1) {
/* 107 */       visitJarReportChildren(jarReport, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitJarReportChildren(JarReport jarReport, int _field, int _type) {
/* 112 */     switch (_field) {
      case 1:
/* 114 */         jarReport.setDomain(readString());
        break;
      case 2:
/* 117 */         jarReport.setStartTime(readDate());
        break;
      case 3:
/* 120 */         jarReport.setEndTime(readDate());
        break;
      case 33:
/* 123 */         if (_type == 1) {
/* 124 */           Domain domain = new Domain();

/* 126 */           visitDomain(domain);
/* 127 */           this.m_linker.onDomain(jarReport, domain); break;
/* 128 */         }  if (_type == 2) {
/* 129 */           for (int i = readInt(); i > 0; i--) {
/* 130 */             Domain domain = new Domain();

/* 132 */             visitDomain(domain);
/* 133 */             this.m_linker.onDomain(jarReport, domain);
          }
        }
        break;
    }
  }



  public void visitMachine(Machine machine) {
    byte tag;
/* 144 */     while ((tag = readTag()) != -1) {
/* 145 */       visitMachineChildren(machine, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitMachineChildren(Machine machine, int _field, int _type) {
/* 150 */     switch (_field) {
      case 1:
/* 152 */         machine.setId(readString());
        break;
      case 33:
/* 155 */         if (_type == 1) {
/* 156 */           Jar jar = new Jar();

/* 158 */           visitJar(jar);
/* 159 */           this.m_linker.onJar(machine, jar); break;
/* 160 */         }  if (_type == 2) {
/* 161 */           for (int i = readInt(); i > 0; i--) {
/* 162 */             Jar jar = new Jar();

/* 164 */             visitJar(jar);
/* 165 */             this.m_linker.onJar(machine, jar);
          }
        }
        break;
    }
  }

  private Date readDate() {
    try {
/* 174 */       return new Date(readVarint(64));
/* 175 */     } catch (IOException e) {
/* 176 */       throw new RuntimeException(e);
    }
  }

  private int readInt() {
    try {
/* 182 */       return (int)readVarint(32);
/* 183 */     } catch (IOException e) {
/* 184 */       throw new RuntimeException(e);
    }
  }

  private String readString() {
    try {
/* 190 */       return this.m_in.readUTF();
/* 191 */     } catch (IOException e) {
/* 192 */       throw new RuntimeException(e);
    }
  }

  private byte readTag() {
    try {
/* 198 */       return this.m_in.readByte();
/* 199 */     } catch (IOException e) {
/* 200 */       throw new RuntimeException(e);
    }
  }

  protected long readVarint(int length) throws IOException {
/* 205 */     int shift = 0;
/* 206 */     long result = 0L;

/* 208 */     while (shift < length) {
/* 209 */       byte b = this.m_in.readByte();
/* 210 */       result |= (b & 0x7F) << shift;
/* 211 */       if ((b & 0x80) == 0) {
/* 212 */         return result;
      }
/* 214 */       shift += 7;
    }

/* 217 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/jar/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */